package gov.jyy.operationlog.config;

import gov.jyy.operationlog.aop.OperationLogAspect;
import gov.jyy.operationlog.core.DefaultOperationLogHandle;
import gov.jyy.operationlog.core.OperationLog;
import gov.jyy.operationlog.core.OperationLogHandle;
import gov.jyy.operationlog.core.SendLogInterface;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Configuration
@ConditionalOnClass(OperationLog.class)
@ConditionalOnProperty(name = "operationlog.enable", havingValue = "true")
@Import(OperationLogAspect.class)
public class LogConfiguration implements ApplicationContextAware {

    private ApplicationContext applicationContext;
    @Resource
    SendLogInterface sendLogInterface;

    @Resource
    private HttpServletRequest request;

    @Bean
    public OperationLogHandle defaultOperationLogHandle() {
        return new DefaultOperationLogHandle(sendLogInterface, request, applicationContext);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}